#!/bin/env bash
#
# Cucumber-Chef v<%= Cucumber::Chef::VERSION %> Bootstrap Script
#
# Generated <%= Time.now.utc %>
#
set -x
export DEBIAN_FRONTEND="noninteractive"
CHEF_SOLO_ROOT="/tmp/chef-solo"
KNIFE_CONFIG_EXP_FILE="/tmp/knife-config.exp"
cd ${CHEF_SOLO_ROOT}

grep "<%= @lab_user %>" /etc/sudoers
if [ $? -eq 1 ]; then
  echo "<%= @lab_user %> ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers
fi

echo "127.0.0.1 <%= @hostname_full %> <%= @hostname_short %>" | tee -a /etc/hosts
echo "<%= @hostname_full %>" | tee /etc/hostname
hostname <%= @hostname_full %>

apt-get -y --force-yes update
apt-get -y --force-yes install build-essential expect

mkdir -pv /etc/chef /var/log/chef ${HOME}/.chef

cat <<EOF | tee /etc/chef/solo.rb
file_cache_path "${CHEF_SOLO_ROOT}/"
cookbook_path %w(${CHEF_SOLO_ROOT}/cookbooks/ ${CHEF_SOLO_ROOT}/site-cookbooks/)
role_path "${CHEF_SOLO_ROOT}/roles/"
EOF

cat <<EOF | tee ${CHEF_SOLO_ROOT}/attributes.json
<%= @chef_solo_attributes.to_json %>
EOF

wget -v http://www.opscode.com/chef/install.sh
bash install.sh -v <%= @chef_version %>
<% if (@chef_pre_11 == false) -%>

mkdir -pv /var/chef/cache ${CHEF_SOLO_ROOT}/cookbooks/chef-server
wget -qO- https://github.com/opscode-cookbooks/chef-server/archive/master.tar.gz | tar xvzC ${CHEF_SOLO_ROOT}/cookbooks/chef-server --strip-components=1
<% else -%>

export PATH=/opt/chef/embedded/bin:$PATH

cat <<EOF | tee ${CHEF_SOLO_ROOT}/Gemfile
source 'https://rubygems.org'
gem 'chef', '<%= @chef_version %>'
gem 'berkshelf'
gem 'moneta', '< 0.7.0'
EOF

cat <<EOF | tee ${CHEF_SOLO_ROOT}/Berksfile
site :opscode
cookbook 'chef-server'
EOF

gem install bundler --no-ri --no-rdoc
bundle install --standalone --path vendor/bundle --binstubs
mv -v ${CHEF_SOLO_ROOT}/cookbooks ${CHEF_SOLO_ROOT}/site-cookbooks
bin/berks install --path ${CHEF_SOLO_ROOT}/cookbooks/
<% end -%>

chef-solo --config /etc/chef/solo.rb --json-attributes ${CHEF_SOLO_ROOT}/attributes.json --logfile /var/log/chef/chef-solo.log --log_level debug
<% if (@chef_pre_11 == false) -%>

cat << EOF | tee /etc/chef-server/chef-server.rb
#
# Auto-generated by Cucumber-Chef v#{Cucumber::Chef::VERSION} -- DO NOT EDIT!
#
server_name = "<%= @server_name %>"
#
topology                "standalone"
api_fqdn                server_name
nginx['url']          = "https://#{server_name}"
nginx['server_name']  = server_name
lb['fqdn']            = server_name
bookshelf['vip']      = server_name
EOF
sudo chef-server-ctl reconfigure
<% end -%>

echo -n "Waiting on <%= File.basename(@chef_validator) %> and <%= File.basename(@chef_webui) %> to appear..."
until [ -f <%= @chef_validator %> ] && [ -f <%= @chef_webui %> ]; do
  echo -n "."
  sleep 1
done
echo "done."

cp -v <%= @chef_validator %> <%= @chef_webui %> ~/.chef
<% if (@chef_pre_11 == false) -%>

ln -sv <%= @chef_validator %> /etc/chef/validation.pem
ln -sv <%= @chef_admin %> /etc/chef/admin.pem
<% end -%>

cat <<EOF | tee ${KNIFE_CONFIG_EXP_FILE}
#!/usr/bin/expect -f
set timeout 10
<% if (@chef_pre_11 == false) -%>
spawn knife configure -i --server-url https://127.0.0.1 --admin-client-key <%= @chef_admin %> -u ${SUDO_USER} -r '' --defaults --yes -VV
<% else -%>
spawn knife configure -i --server-url https://127.0.0.1 -u ${SUDO_USER} -r '' --defaults --yes -VV
<% end -%>
expect "Please enter a password for the new user:" { send "<%= @default_password %>\n" }
interact
EOF
chmod +x ${KNIFE_CONFIG_EXP_FILE}
${KNIFE_CONFIG_EXP_FILE}

knife client create <%= @user %> -a -f ${HOME}/.chef/<%= @user %>.pem --disable-editing --yes -VV
chown -Rv ${SUDO_USER}:${SUDO_USER} ${HOME}
